const fs = require('fs')
// 使用正则表达式匹配注释
const RemarkReg = /\/\*[^]*?\*\//g;

let markdown = '## 配置文件说明\n';

fs.readdir('../../interfaces/configs/',(err,files)=>{
    if(err){
        throw err;
    }
    // 过滤出TS文件
    let tsFiles = files.filter(async (file)=>{
        return file.endsWith('.ts')
    })

    if(!tsFiles.length){
        console.error('未找到TS文件！');
        return;
    }
    // 读取TS文件
    tsFiles.map((filename,index)=>{
        fs.readFile(`../../interfaces/configs/${filename}`,(err,data) => {
            console.log(111);
            if(err){
                console.log(err)
                return;
            }
            const remarks = data.toString().match(RemarkReg)[0]
            if(remarks.length > 0){
                const remark = remarks.split('@')
                const newRemark = []
                let file;
                let title;
                let desc;
                let version;
                remark.shift()
                remark.forEach((item)=>{
                    let temp;
                    temp = item.replace(/\r\n\s\D/g,"")
                    newRemark.push(temp)
                })
                file = newRemark[2].split(' ')[1]
                title = newRemark[0].split(' ')[1]
                desc = newRemark[1].split(' ')[1]
                if(newRemark.length === 4){
                    version = newRemark[3].split(' ')[1] 
                    version = version.split('/')[0]
                }
                // 写入Markdown文件
                markdown += `### ${index+1}. ${file}(${title}) \n`;
                markdown += `<font color="#808080">${desc}</font> \n`;
                markdown +=  '```' + 'TypeScript\n' + data + '\n``` \n';
                markdown += `*** \n`;

                // 读取json文件 
                if(!version){
                    cfgFile = file + '.json5';
                    fs.readFile(`../../interfaces/xxx/${cfgFile}`,'utf-8',(err,data)=>{
                        markdown += `配置文件: ${cfgFile} \n`;
                        markdown += '```' + 'json\n' + data + '\n``` \n';
                        markdown += `*** \n`;
    
                        if(index + 1 === tsFiles.length){
                            fs.writeFile('../../MarkDown/exportMd1.md',markdown,err => {
                                if (err) {
                                    console.error(err);
                                } else {
                                    console.log('Markdown文件生成成功！');
                                }
                            });
                        }
                    })
                }else{
                    cfgFile = file + '@' + version + '.json5';
                    fs.readFile(`../../interfaces/xxx/${cfgFile}`,'utf-8',(err,data)=>{
                        console.log(222);
                        markdown += `配置文件: ${cfgFile} \n`;
                        markdown += '```' + 'json\n' + data + '\n``` \n';
                        markdown += `*** \n`;
    
                        if(index + 1 === tsFiles.length){
                            fs.writeFile('../../MarkDown/exportMd1.md',markdown,err => {
                                if (err) {
                                    console.error(err);
                                } else {
                                    console.log('Markdown文件生成成功！');
                                }
                            });
                        }
                    })
    
                }
            }
        })
    })
})



